package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.DOtherCostEntity;
import com.hongxun.modules.bss.service.DOtherCostService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 点位其他工程量表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-04 10:44:03
 */
@RestController
@RequestMapping("/bss/dothercost")
public class DOtherCostController {
	@Autowired
	private DOtherCostService dOtherCostService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";

		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
		{
			wheres+=" and  p_p_guid = '" + query.get("dpGuid").toString()+"' ";
		}
		if(query.get("qWork")!=null&&!query.get("qWork").toString().equals(""))
		{
			wheres+=" and  o_c_work = '" + query.get("qWork").toString()+"' ";
		}
		if(query.get("qName")!=null&&!query.get("qName").toString().equals(""))
		{
			wheres+=" and  o_c_name like '%" + query.get("qName").toString()+"%' ";
		}
		wheres+=" and  c_c_datastate != '删除' ";
        query.put("filterSql",wheres+" ");
		List<DOtherCostEntity> dOtherCostList = dOtherCostService.queryList(query);
		int total = dOtherCostService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dOtherCostList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		DOtherCostEntity dOtherCost = dOtherCostService.queryObject(guid);
		
		return R.ok().put("dOtherCost", dOtherCost);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody DOtherCostEntity dOtherCost){
		dOtherCost.setOCPriceall(Priceall(dOtherCost.getOCPrice(), dOtherCost.getOQCount()));
		dOtherCostService.save(dOtherCost);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody DOtherCostEntity dOtherCost){
		dOtherCost.setOCPriceall(Priceall(dOtherCost.getOCPrice(), dOtherCost.getOQCount()));
		dOtherCostService.update(dOtherCost);
		
		return R.ok();
	}

	/**
	 * 修改数量和单价
	 * @param Parms
	 * @return
	 */
	@RequestMapping("/updateCell")
	public R update(@RequestParam Map<String ,Object> Parms){

		DOtherCostEntity dOtherCost = dOtherCostService.queryObject(Parms.get("guid").toString());
		BigDecimal Price = new BigDecimal(Parms.get("oCPrice").toString());
		BigDecimal Count = dOtherCost.getOQCount();
		dOtherCost.setOCPrice(Price);
		dOtherCost.setOCPriceall(Priceall(Price,Count));
		dOtherCost.setCCDatastate("修改");
		dOtherCostService.update(dOtherCost);

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		dOtherCostService.deleteBatch(guids);
		
		return R.ok();
	}

	private static BigDecimal Priceall(BigDecimal Price,BigDecimal Count){
		BigDecimal Priceall = null;
		Priceall = Price.multiply(Count);
		return Priceall;
	}
	
}
